//
//  TMGChangePwdViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/8.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGACResetPwdViewController.h"
#import "UITextField+STInputLimit.h"
#import "STSendButton.h"
@interface TMGACResetPwdViewController ()<STSendButtonDlegate>;
@property(nonatomic, strong) UITextField                     *codeTextField;
@property(nonatomic, strong) UITextField                     *nowPwdTextField;
@property(nonatomic, strong) UITextField                     *confimPwdTextField;
@property(nonatomic, strong) STSendButton                     *sendButton;
@property(nonatomic, strong) NSArray<STTableEasyModel*>                     *dataSource;
@end

@implementation TMGACResetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    [self configSubView];
    [self addNotifacations];
    [self configFooterView];
    // Do any additional setup after loading the view.
}
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(UITextFieldTextDidChange:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
}
- (void)UITextFieldTextDidChange:(NSNotification*)notify{

    if (notify.object == self.nowPwdTextField) {
        [self.nowPwdTextField st_textInputLimitWithLength:16];
    }
    if (notify.object == self.confimPwdTextField) {
        [self.confimPwdTextField st_textInputLimitWithLength:16];
    }
}
- (void)dealloc{
    [self.sendButton timerEnd];
    self.sendButton = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark --subView
- (void)configSubView{
    self.codeTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 20)];
    self.codeTextField.placeholder = @"请输入验证码";
    self.codeTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    self.sendButton = [[STSendButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30) andWithDuration:60];
    self.sendButton.backgroundColor = TM_orangeColor;
    self.sendButton.layer.cornerRadius = 15;
    self.sendButton.clipsToBounds = YES;
    self.sendButton.delegate = self;
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:12];
    self.codeTextField.rightView = self.sendButton;
    [self.sendButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    self.codeTextField.rightViewMode = UITextFieldViewModeAlways;
    
    
    self.nowPwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 20)];
    self.nowPwdTextField.placeholder = @"请输入6~16位的密码";
    
    self.confimPwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 20)];
    self.confimPwdTextField.placeholder = @"请确认新密码";
    
    self.dataSource = @[
                        [[STTableEasyModel alloc] initWithTextString:@"验证码" accessoryView:self.codeTextField],
                        [[STTableEasyModel alloc] initWithTextString:@"新密码" accessoryView:self.nowPwdTextField],
                        [[STTableEasyModel alloc] initWithTextString:@"确认新密码" accessoryView:self.confimPwdTextField]];
    [self.tableView reloadData];
    
}
- (void)configFooterView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, 0, 200, 44)
                                                     text:@"新密码不能和原密码相同"
                                                textColor:TM_secendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [footer addSubview:titleLable];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60 , 44)
                                                        title:@"确认重置"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:TM_orangeColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedLogoutButton];
    }];
    confimButton.st_centerY = 100;
    
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    footer.height = confimButton.bottom + 20;
    self.tableView.tableFooterView = footer;
}
- (void)onSelectedLogoutButton{
    if (!self.codeTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        return ;
    }
    if (!self.nowPwdTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入新密码"];
        return ;
    }
    if (!self.confimPwdTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请再次输入密码"];
        return ;
    }
    if (self.nowPwdTextField.text.length < 6) {
        [SVProgressHUD showErrorWithStatus:@"密码不能小于6位"];
        return ;
    }
    if (![self.confimPwdTextField.text isEqualToString:self.nowPwdTextField.text]) {
        [SVProgressHUD showErrorWithStatus:@"两次密码输入不一致"];
        return ;
    }
    [self sendChangeRequest];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    NSString * phone = [STUserManger defult].loginUser.mobile;
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth - 30, 44)
                                                     title:[NSString stringWithFormat:@"您的手机%@会收到一条短信",phone]
                                                titleColor:TM_secendTextColor
                                                 titleFont:14
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"关于我们拷贝"]];
    [sectionHeader addSubview:buyButton];
    buyButton.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    return sectionHeader;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    STTableEasyModel * model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.textString;
    cell.detailTextLabel.text = model.detailString;
    if (model.accessoryView) {
        cell.accessoryView = model.accessoryView;
    }else{
        cell.accessoryType = model.accessoryType;
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --STSendButtonDlegate
- (BOOL)STSendButtonWillClic:(UIButton *)button{

    return YES;
}
- (void)STSendButtonDidClic:(UIButton*)button isFirstClic:(BOOL)isFirstClic duration:(NSInteger)duration{
    
    
    if (isFirstClic) {
        [self sendcodeRequest];
    }
}

- (void)STSendButtonDidCountdown:(UIButton*)button duration:(NSInteger)duration{
    [self.sendButton setTitle:[NSString stringWithFormat:@"%ld秒",duration] forState:UIControlStateNormal];
}
- (void)STSendButtonTimeEnded:(UIButton*)button{
    [self.sendButton timerEnd];
    [self.sendButton setTitle:@"获取验证码" forState:UIControlStateNormal];
}
#pragma mark --NetWork Method
- (void)sendcodeRequest{
     NSString * phone = [STUserManger defult].loginUser.mobile;
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:phone forKey:@"mobile"];
 
    [SVProgressHUD showWithStatus:@"正在发送"];
    [[STNetWrokManger defaultClient] requestWithPath:[NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/index/sendSms"]
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"验证码已成功发送"];
                                                 [self.sendButton timerBegin];
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [self.sendButton timerEnd];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
- (void)sendChangeRequest{
    [SVProgressHUD showWithStatus:@"正在重置密码"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if (self.codeTextField.text.length) {
        [dic setObject:self.codeTextField.text forKey:@"code"];
    }
    if (self.nowPwdTextField.text.length) {
        [dic setObject:self.nowPwdTextField.text forKey:@"password"];
    }
    NSString * phone = [STUserManger defult].loginUser.mobile;
    [dic setObject:phone forKey:@"mobile"];
    [[STNetWrokManger defaultClient] requestWithPath:[NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/index/forgetPassword"]
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"密码重置成功"];
                                                 [self.navigationController popToRootViewControllerAnimated:YES];
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end


